using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CloudWars.Tiles;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace CloudWars
{
    public class Grid
    {
        public static Texture2D OverlayTexture { get; set; }
        

        public int TileSize
        {
            get;
            set;
        }

        public int GridWidth
        {
            get;
            set;
        }

        public int GridHeight
        {
            get;
            set;
        }

        public Tile[,] gridLayout { get; set; }
        public Tile[,] grassLayout { get; set; }

        public Grid(int gridWidth, int gridHeight, int tileSize)
        {
            GridWidth = gridWidth;
            GridHeight = gridHeight;
            TileSize = tileSize;

            gridLayout = InitializeGridWithMapTiles(GridWidth, GridHeight);
            grassLayout = InitializeGridWithDefaultTiles(GridWidth, GridHeight);
        }

        public static void LoadTextures(ContentManager contentManager)
        {
            OverlayTexture = contentManager.Load<Texture2D>("TileOverlay");
        }

        public void AddTileAtLocation(Tile toAdd, int gridXPos, int gridYPos)
        {
            gridLayout[gridXPos, gridYPos] = toAdd;
        }

        public void Draw(SpriteBatch spriteBatch, List<Point> movementTilesToTint, List<Point> attackTilesToTint)
        {

            for (int i = 0; i < GridWidth; i++)
            {
                for (int j = 0; j < GridHeight; j++)
                {
                    var tilePositionOnScreen = new Rectangle(TileSize * i,
                                                TileSize * j,
                                                TileSize,
                                                TileSize);

                    Color movementOverlayColor = Color.White;

                    if (movementTilesToTint.Contains(new Point(i, j)))
                    {
                        movementOverlayColor = Color.SkyBlue;
                    }

                    if (attackTilesToTint.Contains(new Point(i, j)))
                    {
                        movementOverlayColor = Color.HotPink;
                    }
                    else
                    {
                        //movementOverlayColor = Color.Gray;
                    }
                    spriteBatch.Draw(grassLayout[i, j].TileTexture, tilePositionOnScreen, Color.White);
                    spriteBatch.Draw(gridLayout[i, j].TileTexture, tilePositionOnScreen, Color.White);
                    if (movementOverlayColor != Color.White)
                    {
                        spriteBatch.Draw(OverlayTexture, tilePositionOnScreen, movementOverlayColor);
                    }
                }
            }
        }

        private Tile[,]InitializeGridWithDefaultTiles(int width, int height)
        {
            Tile[,] tiles = new Tile[width, height];

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    tiles[i, j] = new PlainTile();
                }
            }

            return tiles;
        }

        private Tile[,] InitializeGridWithMapTiles(int width, int height)
        {
            Tile[,] tiles = new Tile[width, height];

            tiles = InitializeGridWithDefaultTiles(width, height);
            tiles[width / 2 + 1, height / 2 -1] = new MountainTile();
            tiles[width / 2 + 2, height / 2 -1] = new MountainTile();
            tiles[width / 2 + 2, height / 2 ] = new MountainTile();
            tiles[width / 2 + 1, height / 2 ] = new MountainTile();
            tiles[width- 1, height- 1] = new WoodsTile();
            tiles[4, 0] = new WoodsTile();
            tiles[4, height- 1] = new WoodsTile();
            tiles[width- 1, 0] = new WoodsTile();
            for (int i = 4; i < width; i++)
            {
                tiles[i, 2] = new RiverTile();
                tiles[i, height - 3] = new RiverTile();
            }
            return tiles;
        }
    }
}
